package com.tekj.cxqcb.view.Module.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.resultBean.GetCarBrandAndModelBean;

import java.util.List;

import common.base.activity.BaseActivity;


/**
 * @author: xp
 * @date: 2017/7/19
 */

public class BrandScreenAdapter extends RecyclerView.Adapter<BrandScreenAdapter.ViewHolder> {
    private LayoutInflater mInflater;
    private List<GetCarBrandAndModelBean.DataBean.BrandListBean> mData;
    private Context mContext;
    private BaseActivity activity;

    private int checkItemPosition = -1;

    public void setCheckItem(int position) {
        checkItemPosition = position;
        notifyDataSetChanged();
    }

    public int getCheckItemPosition() {
        return checkItemPosition;
    }

    public BrandScreenAdapter(Context context, List<GetCarBrandAndModelBean.DataBean.BrandListBean> data, BaseActivity activity) {
        mInflater = LayoutInflater.from(context);
        mData = data;
        this.mContext = context;
        this.activity = activity;
    }

    @Override
    public BrandScreenAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_default_drop_down2, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.tvName = view.findViewById(R.id.text);
        viewHolder.imgLogo = view.findViewById(R.id.img_logo);
        viewHolder.imgXz = view.findViewById(R.id.img_gx);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final BrandScreenAdapter.ViewHolder holder, final int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                }
            });

        }
        if (mData.get(position).getImage() != null && !mData.get(position).getImage().equals(""))
            Picasso.get().load(mData.get(position).getImage())
                    .resize(1080, 1080)
                    .into(holder.imgLogo);

        holder.tvName.setText(this.mData.get(position).getName());

        holder.tvName.setTextColor(mContext.getResources().getColor(
                checkItemPosition == holder.getLayoutPosition() ?
                        R.color.drop_down_selected : R.color.drop_down_unselected));

        holder.imgXz.setVisibility(checkItemPosition == holder.getLayoutPosition() ? View.VISIBLE : View.INVISIBLE);
//        holder.tvName.setBackgroundColor(mContext.getResources().getColor(
//                checkItemPosition == holder.getLayoutPosition() ?
//                        R.color.check_bg : R.color.white));

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    //**********************itemClick************************
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
    //**************************************************************

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        ImageView imgLogo;
        ImageView imgXz;

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }

    /**
     * 提供给Activity刷新数据
     *
     * @param list
     */
    public void updateList(List<GetCarBrandAndModelBean.DataBean.BrandListBean> list) {
        this.mData = list;
        notifyDataSetChanged();
    }

    public Object getItem(int position) {
        return mData.get(position);
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的char ascii值
     */
    public int getSectionForPosition(int position) {
        return mData.get(position).getLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getItemCount(); i++) {
            String sortStr = mData.get(i).getLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

}
